﻿
layui.use('table', function () {
    var table = layui.table
        , form = layui.form;  //启用
    table.render({
        elem: '#test'
        , url: '/Scenicspot/GetScenicspotList'
        , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        , cols: [[
            { type: 'checkbox' }
            , { field: 'Id', width: 60, title: 'ID', sort: true, align: 'center' }
            , { field: 'ScenicName', width: 150, title: '景点名称', align: 'center' }
            , { field: 'Certification', width: 200, title: '景点等级', align: 'center' }
            , { field: 'BeCarefulInfoId', width: 110, title: '注意事项', sort: true, align: 'center' }            
            , { field: 'Context', width: 500, align: 'center', title: '景点描述', templet: '#switchTpl', unresize: true }    
            , { fixed: 'right', title: '操作', width: 195, align: 'center', toolbar: '#barDemo' }
        ]]
        , page: true
        , id: 'testReload'
    });
    //批量删除
    $("#DelListByID").click(function () {
        //获取表格复选框的数据集合 checkStatus('testReload');
        var checkStatus = table.checkStatus('testReload');
        var data = checkStatus.data;
        var ListId = "";
        if (data.length == 0) {
            layer.msg("请选中一行",
                {
                    icon: 0,
                    time: 2000
                });
            return;
        }
        for (var i in data) {
            if (i == data.length - 1) {
                ListId += data[i].Id;
            }
            else {
                ListId += data[i].Id + ",";
            }
        };
        layer.confirm('确认要删除吗？', function () {
            $.ajax({
                url: "/Scenicspot/BatchDeleteById?ListId=" + ListId,
                type: "post",
                success: function (result) {
                    if (result.Success) {
                        layer.msg(result.msg, {
                            icon: 1,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）使用这个弹框那么弹框后续操作就要写在function中
                        }, function () {
                            location.reload();
                        });
                    }
                    else {
                        layer.msg(result.msg, { icon: 0, time: 1000 });
                    }
                }
            })
        });
    });

    //监听工具
    table.on('tool(demo)', function (obj) {
        var data = obj.data;
        //删除
        if (obj.event === 'del') {
            layer.confirm('确认删除行？', function (index) {
                $.ajax({
                    url: "/Scenicspot/DelScenicspot?Id=" + data.Id,
                    type: "post",
                    success: function (result) {
                        if (result.Success) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 2000 //2秒关闭（如果不配置，默认是3秒）使用这个弹框那么弹框后续操作就要写在function中
                            }, function () {
                                location.reload();
                            });
                        }
                        else {
                            layer.msg(result.msg);
                        }
                    }
                })
            });
            //编辑
        } else if (obj.event === 'edit') {
            $("#Id").val(data.Id);
            //$("#Certification").val(data.Certification);
            $("#ScenicName").val(data.ScenicName);
            $("#BeCarefulInfoId").val(data.BeCarefulInfoId);
            $("#Context").val(data.Context);
            $("#Certification option[value='" + data.Certification + "']").attr("selected", "selected");

            //打开编辑路线按钮
            layer.open({
                type: 1,
                title: '编辑景点',
                area: ['700px', ''],
                shadeClose: false,
                content: $('#Rigth'),
            });
            //点击编辑将标题修改成编辑
            $("#rigthName").text("编辑景点");
        }
    });
});

$(function () {
    //打开添加景点按钮
    $("#AddScenicspot").on('click', function () {
        layer.open({
            type: 1,
            title: '添加景点',
            area: ['600px', ''],
            shadeClose: false,
            content: $('#Rigth'),
        });
        //点击添加清空编辑时残留的值
        $("#layui-form input").val("");
        $(".textarea").val("");
        $("#Certification").val("");
        //点击添加将标题修改成添加
        $("#rigthName").text("添加景点");
    });

    //添加或编辑景点
    $("#commit").on('click', function () {        
        var Scenicspot = {};
        Scenicspot.Id = $("#Id").val();
        Scenicspot.Certification = $("#Certification").val();
        Scenicspot.ScenicName = $("#ScenicName").val();
        Scenicspot.BeCarefulInfoId = $("#BeCarefulInfoId").val();
        Scenicspot.Context = $("#Context").val();
        //根据ID是否为空判断添加还是编辑
        $.ajax({
            url: "/Scenicspot/AddScenicspot",
            type: "post",
            data: Scenicspot,
            success: function (result) {
                if (result.Success) {
                    layer.msg(result.msg, {
                        icon: 1,
                        time: 1000 //1秒关闭（如果不配置，默认是3秒）使用这个弹框那么弹框后续操作就要写在function中
                    }, function () {
                        location.reload();
                    });
                }
                else {
                    layer.msg(result.msg);
                }
            }
        });
    });

    //查询按钮
    $("#SelscernicName").on('click', function () {
        var table = layui.table;
        var data = $("#scernicName").val();
        table.reload('testReload', {
            where: { //设定异步数据接口的额外参数，任意设
                data: data,
                //…
            }
        });
    });
});